/**需求描述：
 定义方法 getNum(String type, int... numbers)，根据 type 参数对可变参数 numbers 执行指定计算：
 支持操作类型："sum"（求和）、"avg"（算术平均）、"max"（最大值）、"min"（最小值）等
 处理空参数集时返回 0
 输出格式示例：getNum("avg", 2,4,6) → 输出 平均值是：4.0
 */
public class Test5 {

    /**
     * 根据指定类型对数字集合进行计算
     * @param type 计算类型：sum, avg, max, min
     * @param numbers 可变参数数字集合
     * @return 计算结果
     */
    public static double getNum(String type, int... numbers) {
        if (numbers == null || numbers.length == 0) {
            return 0.0;
        }
        switch (type.toLowerCase()) {
            case "sum":
                return calculateSum(numbers);
            case "avg":
                return calculateAverage(numbers);
            case "max":
                return calculateMax(numbers);
            case "min":
                return calculateMin(numbers);
            default:
                throw new IllegalArgumentException("不支持的计算类型: " + type);
        }
    }
    private static int calculateSum(int[] numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }
    private static double calculateAverage(int[] numbers) {
        return (double) calculateSum(numbers) / numbers.length;
    }
    private static int calculateMax(int[] numbers) {
        int max = numbers[0];
        for (int num : numbers) {
            if (num > max) {
                max = num;
            }
        }
        return max;
    }

    private static int calculateMin(int[] numbers) {
        int min = numbers[0];
        for (int num : numbers) {
            if (num < min) {
                min = num;
            }
        }
        return min;
    }
    public static void main(String[] args) {
        // 测试用例
        System.out.println("平均值是：" + getNum("avg", 2, 4, 6));  // 4.0
        System.out.println("总和是：" + getNum("sum", 1, 3, 5));    // 9
        System.out.println("最大值是：" + getNum("max", 7, 2, 9));  // 9
        System.out.println("最小值是：" + getNum("min", 4, 1, 5));  // 1
        System.out.println("空参数结果：" + getNum("sum"));          // 0.0
    }
}